home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4044 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.6 KB

  1. Path: news.compuserve.com!newsmaster
  2. From: 73700.776@compuserve.com (Walter C. Riley)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Output precision
  5. Date: Sat, 27 Jan 1996 13:51:04 GMT
  6. Organization: CompuServe Incorporated
  7. Message-ID: <4edamo$fil@dub-news-svc-2.compuserve.com>
  8. References: <rplDLKLwK.119@netcom.com>
  9. NNTP-Posting-Host: ad60-032.compuserve.com
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. rpl@netcom.com (Robert Laudati) wrote:
  13.  
  14. >It doesn't get too much more basic than this, but I can not
  15. >set the precision correctly on output. Here's the sample:
  16.  
  17. >#include <stdio.h>
  18. >#include <iostream.h>
  19. >#include <iomanip.h>
  20. >int main( int argc, char *argv[])
  21. >{
  22. >  int    i=0;
  23. >  float  x=56.0;
  24. >  double y=34.038383999;
  25. >  i = cout.precision(6);
  26. >  printf( "%d %f %lf\n\n", i, x, y );
  27. >  cout << i << " " << x << " " << y << endl;
  28. >  return 0;
  29. >}
  30.  
  31. >Which results in:
  32.  
  33. >robl@cosmo<216>: test
  34. >6 56.000000 34.038384
  35. >6 56 34.0384
  36. >robl@cosmo<217>: 
  37.  
  38. >I'm running gcc 2.7.1 on Solaris 2.4 - and very confused!
  39.  
  40.  
  41. Rob,
  42.  
  43. If you are trying to match the printf output exactly, I can't help
  44. you.  You can use cout.setf(ios::showpoint) to force the decimal on
  45. the 56, but I don't know of a simple way to force 6 zeros after the
  46. decimal using built-in mainipulators.  Hopefully someone that does
  47. will jump in.  Perhaps ios::precision(int) might better have been
  48. named significant_digits.  
  49.  
  50. You can also use the manipulators setiosflags and resetiosflags to
  51. change the flags between the variables in the stream output statement
  52. --on the fly, so to speak.  I only mention it because you included
  53. iomanip.h but didn't use manipulators.
  54.  
  55. Good luck,
  56. Walt
  57.  
  58.  
  59.